#pragma once

class config
{
public:
	wstring m_strBaseDir;

	static config& instance();
};

class path_helper{
	wstring m_strLibPath;	//vector?

public:
	static bool file_exist(wstring aStrPath)
	{
		return (::GetFileAttributes(aStrPath.c_str()) != -1);
	}

	static bool dir_exist(wstring aStrPath)
	{
		bool bRet = false;
		WIN32_FIND_DATA   wfd;
		HANDLE   hFind=FindFirstFile(aStrPath.c_str(),&wfd);     
		if(hFind != INVALID_HANDLE_VALUE)
		{
			if(wfd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
				bRet = true;
		}
		FindClose(hFind);
		return bRet;
	}

	static wstring process_dir(wstring aStrPath)
	{
	}

	static wstring process_file(wstring aStrPath)
	{
		if (aStrPath.length() <= 0) return aStrPath;
		if (file_exist(aStrPath))
		{
			return aStrPath;
		}

		wstring root = config::instance().m_strBaseDir;
		if (root[root.length() - 1] == L'\\' || root[root.length() - 1] == L'/')
		{
			root.erase(root.length() - 1);
		}

		wstring ret;
		wchar_t fst = aStrPath[0];
		if ( fst == L'\\' || fst == L'/')		
			ret = root + aStrPath;
		else
			ret = root + L"\\" + aStrPath;
		return ret;
	}

	static wstring process_file(wstring aStrTargetPath, wstring aStrCurrentFile)
	{
		if (aStrTargetPath.length() <= 0) return aStrTargetPath;
		
		if (file_exist(aStrTargetPath))
		{
			return aStrTargetPath;
		}

		if (aStrCurrentFile.length() > 0)
		{
			int pos1 = aStrCurrentFile.find_last_of(L'\\');
			int pos2 = aStrCurrentFile.find_last_of(L'/');
			if (pos1 != wstring::npos || pos2 != wstring::npos)
			{
				wstring strCurDir = aStrCurrentFile;
				strCurDir.erase(pos1 > pos2? pos1:pos2);

				wstring ret;
				if ( aStrTargetPath[0] == L'\\' || aStrTargetPath[0] == L'/')	
					ret = strCurDir + aStrTargetPath;
				else
					ret = strCurDir + L"\\" + aStrTargetPath;
				if (file_exist(ret))
				{
					return ret;
				}				
			}
		}
		return process_file(aStrTargetPath);
	}
};